import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/lf_string_ext.dart';
import 'package:largefriends/utils/lf_scroll_behavior.dart';
import 'package:largefriends/widgets/lf_app_bar.dart';

import '../../../config/lf_config.dart';
import '../../../res/lf_color.dart';
import '../../../res/lf_fonts.dart';
import '../../../res/lf_text_style.dart';
import '../../../res/strings/lf_strings.dart';
import 'lf_edit_text_logic.dart';

class LfEditTextPage extends StatefulWidget {
  const LfEditTextPage({super.key});

  @override
  State<LfEditTextPage> createState() => _LfEditTextPageState();
}

class _LfEditTextPageState extends State<LfEditTextPage> {
  final logic = Get.put(LfEditTextLogic());

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        appBar: lfAppBar(
            title: logic.title,
            actions: [
              InkWell(
                child: Container(
                  alignment: Alignment.center,
                  padding: EdgeInsets.symmetric(horizontal: 16.w),
                  child: Obx(() {
                    bool isEnable =
                        logic.content.value.trim().length >= logic.minNum &&
                            logic.content.value.trim() != logic.origin;
                    return Text(
                      strSave.tr.toUpperCase(),
                      style: lfTextStyle(
                          color: isEnable
                              ? colorFF007F
                              : Colors.transparent,
                          fontFamily: fontBold,
                          sp: 18.sp),
                    );
                  }),
                ),
                onTap: () {
                  if (logic.content.value.trim().length >= logic.minNum &&
                      logic.content.value.trim() != logic.origin) {
                    logic.save();
                  }
                },
              )
            ],
            leadingCallback: () {
              logic.onBackPress();
            }),
        body: ScrollConfiguration(
          behavior: LfScrollBehavior(),
          child: SingleChildScrollView(
            child: Column(
              children: [
                Container(
                  margin:
                      EdgeInsets.symmetric(horizontal: 10.w, vertical: 20.h),
                  padding:
                      EdgeInsets.symmetric(horizontal: 10.w, vertical: 10.h),
                  decoration: BoxDecoration(
                    color: color8F8F8F,
                    borderRadius: 10.r.radius,
                  ),
                  width: ScreenUtil().screenWidth,
                  child: Column(
                    children: [
                      TextField(
                          maxLines: 12,
                          minLines: 8,
                          autofocus: false,
                          inputFormatters: [
                            LengthLimitingTextInputFormatter(logic.maxNum),
                            FilteringTextInputFormatter.deny(RegExp(strEmoji))
                          ],
                          style: lfTextStyle(
                            fontFamily: fontMedium,
                            sp: 16.sp,
                          ),
                          focusNode: logic.focusNode,
                          obscureText: false,
                          controller: logic.controller,
                          textInputAction: TextInputAction.none,
                          textCapitalization: TextCapitalization.sentences,
                          keyboardType: TextInputType.text,
                          onChanged: (value) {
                            logic.content.value = value.trim();
                          },
                          decoration: InputDecoration(
                              isCollapsed: true,
                              //去掉莫名其妙的padding
                              contentPadding: EdgeInsets.zero,
                              suffixIcon: null,
                              enabledBorder: null,
                              border: InputBorder.none,
                              hintStyle: lfTextStyle(
                                  fontFamily: fontMedium,
                                  sp: 16.sp,
                                  color: color514F62),
                              hintText: logic.hint.value,
                              focusedBorder: null)),
                      8.h.sbh,
                      Row(
                        children: [
                          Expanded(child: Obx(() {
                            return Visibility(
                              visible: logic.content.value.trim().length <
                                  logic.minNum,
                              child: Text(
                                strCharactersMinimum.tr.replacePercentS(
                                        [logic.minNum.toString()]) ??
                                    "",
                                style: lfTextStyle(
                                  sp: 14.sp,
                                  fontFamily: fontRegular,
                                ),
                              ),
                            );
                          })),
                          Obx(() => Text(
                              '${logic.content.value.trim().length}/${logic.maxNum}',
                              style: lfTextStyle(
                                sp: 14.sp,
                                fontFamily: fontRegular,
                              )))
                        ],
                      ),
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
      ),
      onWillPop: () async {
        logic.onBackPress();
        return false;
      },
    );
  }
}
